<?php
/**
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/23
 * Time: 17:37
 */

namespace app\admin\controller;
use think\Db;
use think\Loader;
use think\Request;

class AdPosition extends Admin
{
    public function lst($keyword = '')
    {
        $adPositionModel =  model('AdPosition');
        $data = $adPositionModel -> search(trim($keyword));
        $this->assign('adPosition', $data['data']);
        $this->assign('page', $data['page']);
        $this->assign('keyword', input('keyword'));
        return $this->fetch();
    }

    public function add()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('AdPosition');
            $adPosition = input('post.');
            if($validate->check($adPosition))
            {
                $res = Db::name('ad_position')->insert($adPosition);
                if($res)
                {
                    $this->success('添加成功', url('lst'));
                }
                else
                {
                    $this->success('添加失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 更改
     * @param $id
     */
    public function save($id)
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('AdPosition');
            $adPosition = input('post.');
            if($validate->check($adPosition))
            {
                $res = Db::name('ad_position')->update($adPosition);
                if($res)
                {
                    $this->success('编辑成功', url('lst'));
                }
                else
                {
                    $this->success('编辑失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        // 取出要修改的记录
        $adPosition =  model('AdPosition');
        $data =  $adPosition->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


    /**
     * 删除
     */
    public function delete($id)
    {
        $adPosition = db("ad_position");
        $adPosition->delete($id);
        //删除改广告位下的广告
        $this->success('删除成功！');
    }
}